package com.bdqn.xiangchuan.web.controller;



import com.bdqn.xiangchuan.base.Gcmc;
import com.bdqn.xiangchuan.service.GcmcService;
import com.bdqn.xiangchuan.utils.PageUtil;
import com.bdqn.xiangchuan.vo.DataGridResult;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;


@Controller
@RequestMapping("gcmc")
public class GcmcController {
	@Autowired
	private GcmcService gcmcService;
	
	@RequestMapping("getAll")
	@ResponseBody
	public DataGridResult getAll(
			@RequestParam(value="page",required=false,defaultValue="1") int page,
			@RequestParam(value="rows",required=false,defaultValue="10") int rows){
		PageHelper.startPage(page,rows);
		return PageUtil.convertToResult(gcmcService.getAll());
	}
	
	/**
	 * http://ip:port/appName/gcmc/getGcmcsByKhId/1001
	 * @return
	 */
	@RequestMapping("getGcmcsByKhId/{khmc_id}")
	@ResponseBody
	public DataGridResult getGcmcsByKhId(
			@PathVariable("khmc_id") Integer khmc_id,
			@RequestParam(value="page",required=false,defaultValue="1") int page,
			@RequestParam(value="rows",required=false,defaultValue="10") int rows){
	PageHelper.startPage(page,rows);
	System.out.println(khmc_id);
	return PageUtil.convertToResult(gcmcService.getGcmcsByKhId(khmc_id));	
	}
	@RequestMapping("add")
	@ResponseBody
	public boolean add(Gcmc gcmc){
		return gcmcService.add(gcmc);		
	}
	@RequestMapping("update")
	@ResponseBody
	public boolean update(Gcmc gcmc){
		return gcmcService.update(gcmc);		
	}
	@PostMapping("delete")
	@ResponseBody
	public boolean delete(@RequestParam("gcmc_ids[]") Integer[] gcmc_ids){
		System.out.println(gcmc_ids[0]);
		System.out.println(Arrays.toString(gcmc_ids));
	    ArrayList<Integer> arrIds=new ArrayList<Integer>();
	    for (int i=0;i<gcmc_ids.length;i++){
	        arrIds.add(gcmc_ids[i]);
	    }
		return gcmcService.delete(arrIds);		
	};	
	@RequestMapping("getGcmcByGcName/{gcmc_name}")
	@ResponseBody
	public DataGridResult getGcmcByGcName(
			@PathVariable("gcmc_name") String gcmc_name,
			@RequestParam(value="page",required=false,defaultValue="1") int page,
			@RequestParam(value="rows",required=false,defaultValue="10") int rows) {
		return PageUtil.convertToResult(gcmcService.getGcmcByGcName(gcmc_name));		
	}

}
